package we.config;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import we.common.CCommonHandle;

public class CConfigHandle {

    private static CConfigHandle _instance;
    private final static Object lock = new Object();
    private Random r = new Random(System.currentTimeMillis());
    private Properties props;
    private ConcurrentHashMap<String, Long> longPropsCaching;
    private ConcurrentHashMap<String, String[]> listPropsCaching;
    private ConcurrentHashMap<String, Properties> propsByGames;

    private CConfigHandle() {
        try {
            longPropsCaching = new ConcurrentHashMap<String, Long>();
            listPropsCaching = new ConcurrentHashMap<String, String[]>();
            props = new Properties();
            props.load(new FileInputStream(new File(System.getProperty("user.dir")
                    + File.separator + "conf"
                    + File.separator
                    + "cluster.conf")));

            // Load games props
            propsByGames = new ConcurrentHashMap<String, Properties>();
            String[] games = props.getProperty("games").split(";");
            for (int i = 0; i < games.length; i++) {
                Properties p = new Properties();
                File f =
                        new File(System.getProperty("user.dir") + File.separator
                        + "conf" + File.separator + games[i] + ".conf");
                if (f.exists()) {
                    p.load(new FileInputStream(f));
                }
                p.setProperty("defaultScore",
                        p.getProperty("defaultScore", "0"));
                propsByGames.put(games[i], p);
            }
        } catch (Exception e) {
            CCommonHandle.writeErrLog(e);
        }
    }

    public static CConfigHandle instance() {
        if (_instance == null) {
            synchronized (lock) {
                if (_instance == null) {
                    _instance = new CConfigHandle();
                }
            }
        }
        return _instance;
    }

    public String get(String name) {
        if (props != null) {
            return props.getProperty(name);
        } else {
            return null;
        }
    }

    public Long getLong(String name) {
        Long result = longPropsCaching.get(name);
        if (result == null) {
            result = new Long(0);
            try {
                result = Long.parseLong(props.getProperty(name));
            } catch (Exception e) {
                CCommonHandle.writeErrLog(e);
            }
            longPropsCaching.put(name, result);
        }
        return result;
    }

    public String getRandom(String name) {
        String[] data = listPropsCaching.get(name);
        if (data == null) {
            data = new String[0];
            try {
                data = props.getProperty(name).split(";");
            } catch (Exception e) {
                CCommonHandle.writeErrLog(e);
            }
            listPropsCaching.put(name, data);
        }
        if (data.length > 0) {
            return data[r.nextInt(data.length)];
        } else {
            return null;
        }
    }

    public String get(String game, String name) {
        if (propsByGames.get(game) != null) {
            return propsByGames.get(game).getProperty(name);
        } else {
            return null;
        }
    }

    public Long getLong(String game, String name) {
        Long result = null;
        try {
            result = Long.parseLong(propsByGames.get(game).getProperty(name));
        } catch (Exception e) {
            CCommonHandle.writeErrLog(e);
        }
        return result;
    }
}
